/**
 * Created by Administrator on 2017/3/7 0007.
 */
var PageText={
    create:function () {
        var page={};
        page.ui=new PIXI.DisplayObjectContainer();

        var graphic_bg = new PIXI.Graphics();
        graphic_bg.beginFill(0x0);
        graphic_bg.drawRect(0,0,pixiStageW,pixiStageH);
        graphic_bg.alpha=0.8;
        // graphic_bg.interactive=true;
        page.ui.addChild(graphic_bg);
        page.score=0;

        var bl_bala=false;

        var arr_img=[];
        var currentImg;
        for(var i=0;i<5;i++)
        {
            console.log(resource)
            var img=new PIXI.Sprite(resource['assets/images/right'+i.toString()+'.png'].texture);
            img.anchor.set(0.5);
            img.x=pixiStageW/2;
            img.y=pixiStageH/2;
            arr_img.push(img);
        }
        var guagua = new PIXI.Graphics();
        guagua.beginFill(0x0);
        guagua.drawRect(0,0,500,500);
        guagua.alpha=0.2;
        page.ui.addChild(graphic_bg);

        var text = new PIXI.Text('哈哈', {
            fontWeight: 'bold',
            fontSize: 30,
            fontFamily: 'Arial',
            fill: '#FFFFFF',
            align: 'center',
            stroke: '#FFFFFF',
            strokeThickness: 0
           });
        text.anchor.set(0.5);
        text.x=pixiStageW/2;
        text.y=pixiStageH/2;
        page.ui.addChild(text);

        var bt_close=new PIXI.Sprite(resource['assets/images/bt_close.png'].texture);
        bt_close.anchor.set(0.5);
        bt_close.x=(pixiStageW-bt_close.texture.width/2)-70;
        bt_close.y=bt_close.texture.height/2+70;
        // bt_close.on('pointerdown',downHandler);
        // bt_close.on('pointerup',upHandler);
        // page.ui.addChild(bt_close);

        page.ui.interactive=true;
        page.ui.on('pointerdown',pageDownHandler);
        bt_close.on('pointermove',pageMoveHandler);
        bt_close.on('pointerup',pageUpHandler);

        page.InRight=function (idx,_score) {
            page.score=_score;
            if(currentImg&&currentImg.parent)
            {
                currentImg.parent.removeChild(currentImg);
            }
            page.ui.alpha=1;
            graphic_bg.alpha=0;
            text.alpha=0;
            text.scale.x=text.scale.y=0;
            bt_close.alpha=0;
            currentImg=arr_img[idx-1];
            currentImg.alpha=0;
            page.ui.addChild(currentImg);
            new TWEEN.Tween({scaleX:0,scaleY:0,alpha:0})
                .to({scaleX:1,scaleY:1,alpha:1},700)
                .onUpdate(function()
                {
                    currentImg.scale.x=this.scaleX;
                    currentImg.scale.y=this.scaleY;
                    currentImg.alpha=this.alpha;
                })
                .onComplete(function () {
                    bt_close.interactive=true;
                })
                .easing(TWEEN.Easing.Back.Out)
                .start();
            text.text='';

            new TWEEN.Tween({alpha:0,y:740})
                .to({alpha:0.7,y:0},1000)
                .onUpdate(function()
                {
                    graphic_bg.alpha=this.alpha;
                    graphic_bg.y=this.y;
                })
                .easing(TWEEN.Easing.Back.Out)
                .start();
            new TWEEN.Tween({scaleX:0,scaleY:0,alpha:0})
                .to({scaleX:1,scaleY:1,alpha:1},700)
                .onUpdate(function()
                {
                    bt_close.scale.x=this.scaleX;
                    bt_close.scale.y=this.scaleY;
                    bt_close.alpha=this.alpha;
                })
                .delay(400)
                .onComplete(function () {
                    bt_close.interactive=true;
                })
                .easing(TWEEN.Easing.Back.Out)
                .start();
        }

        page.In=function (_text) {
            if(currentImg&&currentImg.parent)
            {
                currentImg.parent.removeChild(currentImg);
            }

            page.ui.alpha=1;
            graphic_bg.alpha=0;
            text.alpha=0;
            bt_close.alpha=0;
            text.text=_text;
            if(bl_bala) {
                text.text = "请移步下一个场景";
                bl_bala = false;
            }

            new TWEEN.Tween({alpha:0,y:740})
                .to({alpha:0.7,y:0},1000)
                .onUpdate(function()
                {
                    graphic_bg.alpha=this.alpha;
                    graphic_bg.y=this.y;
                })
                .easing(TWEEN.Easing.Back.Out)
                .start();

            new TWEEN.Tween({scaleX:0,scaleY:0,alpha:0})
                .to({scaleX:1,scaleY:1,alpha:1},700)
                .onUpdate(function()
                {
                    text.scale.x=this.scaleX;
                    text.scale.y=this.scaleY;
                    text.alpha=this.alpha;
                })
                .onComplete(function () {
                    bt_close.interactive=true;
                })
                .delay(200)
                .easing(TWEEN.Easing.Back.Out)
                .start();

            new TWEEN.Tween({scaleX:0,scaleY:0,alpha:0})
                .to({scaleX:1,scaleY:1,alpha:1},700)
                .onUpdate(function()
                {
                    bt_close.scale.x=this.scaleX;
                    bt_close.scale.y=this.scaleY;
                    bt_close.alpha=this.alpha;
                })
                .delay(400)
                .onComplete(function () {
                    bt_close.interactive=true;
                })
                .easing(TWEEN.Easing.Back.Out)
                .start();
        }
        page.Bala=function(){
            console.log("bala!!!!!!");
            bl_bala=true;
            console.log(text.text);
        };

        function upHandler(e) {
            // e.data.originalEvent.preventDefault()
            // e.data.originalEvent.stopImmediatePropagation()
        }
        function downHandler(e) {
            // e.preventDefault();
            // console.log(e.data.originalEvent.stopImmediatePropagation());
            e.data.originalEvent.preventDefault();
            e.data.originalEvent.stopImmediatePropagation();
            bt_close.interactive=false;
            new TWEEN.Tween({alpha:1})
                .to({alpha:0},300)
                .onUpdate(function()
                {
                    page.ui.alpha=this.alpha;
                })
                .onComplete(function () {
                    page.ui.parent.removeChild(page.ui);
                    var event = new CustomEvent("Game_TextOut");
                    document.dispatchEvent(event);

                })
                .start();

            if(page.score==5)
            {
                var event = new CustomEvent("Main_EndIn");
                document.dispatchEvent(event);
            }
        }
        function pageDownHandler(e)
        {
            e.data.originalEvent.preventDefault();
            e.data.originalEvent.stopImmediatePropagation();
            bt_close.interactive=false;
            new TWEEN.Tween({alpha:1})
                .to({alpha:0},300)
                .onUpdate(function()
                {
                    page.ui.alpha=this.alpha;
                })
                .onComplete(function () {
                    page.ui.parent.removeChild(page.ui);
                    var event = new CustomEvent("Game_TextOut");
                    document.dispatchEvent(event);

                })
                .start();

            if(page.score==5)
            {
                var event = new CustomEvent("Main_EndIn");
                document.dispatchEvent(event);
            }
        }
        function pageUpHandler(e)
        {
            e.data.originalEvent.preventDefault();
            e.data.originalEvent.stopImmediatePropagation();
        }
        function pageMoveHandler(e)
        {
            e.data.originalEvent.preventDefault();
            e.data.originalEvent.stopImmediatePropagation();
        }


        return page;
    }
}